<template>
<div class="tinymce-wrap">
    <editor id="tinymce" v-model="value" :init="init"></editor>
</div>
</template>

<script>
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'

import 'tinymce/themes/silver/theme'
import 'tinymce/plugins/image'
import 'tinymce/plugins/imagetools'

import 'tinymce/plugins/link'
import 'tinymce/plugins/code'
import 'tinymce/plugins/table'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/contextmenu'
import 'tinymce/plugins/wordcount'
import 'tinymce/plugins/colorpicker'
import 'tinymce/plugins/textcolor'
import 'tinymce/plugins/codesample'
import 'tinymce/plugins/paste'


import '../../public/tinymce/langs/zh_CN.js'
import axios from "axios";
export default {
    name: 'Tinymce',
    components: {
        Editor
    },
    props: {
        value: {
            type: String,
            default: ''
        },
        disabled: {
            type: Boolean,
            default: false
        },
        plugins: {
            type: [String, Array],
            default: 'lists image imagetools media table wordcount code codesample paste'
        },
        toolbar: {
            type: [String, Array],
            default: 'code | undo redo | formatselect | bold italic forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image media table | removeformat | codesample pastetext paste'
        }
    },
    data() {
        return {
            init: {
                language_url: '/tinymce/langs/zh_CN.js',
                language: 'zh_CN',
                skin_url: '/tinymce/skins/ui/oxide',
                // skin_url: '/tinymce/skins/ui/oxide-dark',//暗色系
                height: 300,
                plugins: this.plugins,
                external_plugins: {
                    'powerpaste': '../../public/tinymce/plugins/powerpaste/plugin.min.js'
                },
                toolbar: this.toolbar,
                branding: true,
                menubar: true,
                paste_data_images: true,
                powerpaste_word_import: 'propmt',// 参数可以是propmt, merge, clear，效果自行切换对比
                powerpaste_html_import: 'propmt',// propmt, merge, clear
                powerpaste_allow_local_images: true,
                images_upload_handler: (blobInfo, success, failure) => {
                    let formData = new FormData();
                    formData.append('file', blobInfo.blob());
                    axios.post(`/api/v1/file/upload?token=${sessionStorage.getItem('accessToken')}`, formData).then(result =>{
                      success(result.data.data);
                    })
                }
            },
            myValue: this.value
        }
    },
    watch: {
        value(newValue) {
            this.myValue = newValue
        },
        myValue(newValue) {
            this.$emit('input', newValue)
        }
    },
    methods: {
        // 添加相关的事件，可用的事件参照文档=> https://github.com/tinymce/tinymce-vue => All available events
        // 需要什么事件可以自己增加
        onClick(e) {
            this.$emit('onClick', e, tinymce)
        },
        // 可以添加一些自己的自定义事件，如清空内容
        clear() {
            this.myValue = ''
        }
    },
    mounted() {
        //配置的初始化
        tinymce.init({})
    },
    beforeDestroy() {
        //销毁
        tinymce.get(this.id).destroy();
    }
}
</script>

<style>
.tox-silver-sink {
    z-index: 4023 !important;
}
</style>
